using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace RideMe.Core
{
    internal sealed class Command : IDisposable 
    {
        private Process _proc = new Process(); 

        public Process Proc
        {
            get { return _proc; }
            set { _proc = value; }
        }
	
        public void ExecuteCommand(string commmandPath, string arguments)
        {
            // Set UseShellExecute to false for redirection
            _proc.StartInfo.UseShellExecute = false;
            _proc.EnableRaisingEvents = true;

            // Redirect the standard output of the command.  
            // This stream is read asynchronously using an event handler.
            _proc.StartInfo.RedirectStandardOutput = true;

            // Redirect standard input as well.  This stream
            // is used synchronously.
            //_proc.StartInfo.RedirectStandardInput = true;

            _proc.StartInfo.CreateNoWindow = true;
            _proc.StartInfo.FileName = commmandPath;
            _proc.StartInfo.Arguments = arguments;
            _proc.Start();

            // Start the asynchronous read of the sort output stream.
            _proc.BeginOutputReadLine();

            // This seems to be the only way to make the exited event fire.
            _proc.WaitForExit(4500);

        }

        public void Dispose()
        {
            if (_proc != null)
            {
                _proc.Dispose();
            }
        }
    }
}
